//
//  SLYJLodingView.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/12.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLYJLodingView.h"

// 整个logo的高度
CGFloat const kLoadingViewSideLength = 35.0;                            // 35
// 线宽
CGFloat const kCircleLineWidth = kLoadingViewSideLength / 7.0;          // 5
// 字母的宽和高
CGFloat const kCircleSideLength = kLoadingViewSideLength * 3.0 / 7.0;   // 15
// 圆角
CGFloat const kCornerRadius = kCircleLineWidth * 0.5;                   // 10

@interface SLYJLodingView()

@property (nonatomic, assign) BOOL isAnimation;

// colors
@property (nonatomic, strong) UIColor *cColor;
@property (nonatomic, strong) UIColor *fColor;
@property (nonatomic, strong) UIColor *pColor;

// layers
@property (nonatomic, weak) CALayer *cLayer;
@property (nonatomic, weak) CALayer *fLayer;
@property (nonatomic, weak) CALayer *pLayer;

// pathLayer
@property (nonatomic, weak) CAShapeLayer *cPathLayer;
@property (nonatomic, weak) CAShapeLayer *fPathLayer;
@property (nonatomic, weak) CAShapeLayer *pPathLayer;

// maskLayers
@property (nonatomic, weak) CALayer *cMaskLayer;
@property (nonatomic, weak) CALayer *fMaskLayer;
@property (nonatomic, weak) CALayer *pMaskLayer;

// maskPathLayer
@property (nonatomic, weak) CAShapeLayer *cMaskPathLayer1;
@property (nonatomic, weak) CAShapeLayer *cMaskPathLayer2;
@property (nonatomic, weak) CAShapeLayer *fMaskPathLayer1;
@property (nonatomic, weak) CAShapeLayer *fMaskPathLayer2;
@property (nonatomic, weak) CAShapeLayer *fMaskPathLayer3;
@property (nonatomic, weak) CAShapeLayer *pMaskPathLayer1;

@end

@implementation SLYJLodingView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self initData];
        
        [self initLayer];
        
        [[self drawLogoC] maskC];
        
        [[self drawLogoF] maskF];
        
        [[self drawLogoP] maskP];
    }
    
    return self;
}

- (void)initData {
    self.cColor = [UIColor colorWithHexString:@"282d41"];
    self.fColor = [UIColor colorWithHexString:@"eb9650"];
    self.pColor = [UIColor colorWithHexString:@"282d41"];
}

- (void)initLayer {
    CALayer *pLayer = [CALayer layer];
    self.pLayer = pLayer;
    [self.layer addSublayer:pLayer];
    
    CALayer *fLayer = [CALayer layer];
    self.fLayer = fLayer;
    [self.layer addSublayer:fLayer];
    
    CALayer *cLayer = [CALayer layer];
    self.cLayer = cLayer;
    [self.layer addSublayer:cLayer];
}

#pragma mark ----- 画C
- (SLYJLodingView *)drawLogoC {
    self.cLayer.frame = CGRectMake(0, kCircleSideLength * 2.0 - kCircleLineWidth * 2, kCircleSideLength, kCircleSideLength);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCornerRadius)];
    [path addArcWithCenter:CGPointMake(kCircleSideLength - kCornerRadius, kCircleSideLength - kCornerRadius) radius:kCornerRadius startAngle:0 endAngle:M_PI_2 clockwise:true];
    [path addLineToPoint:CGPointMake(kCornerRadius, kCircleSideLength)];
    [path addArcWithCenter:CGPointMake(kCornerRadius, kCircleSideLength - kCornerRadius) radius:kCornerRadius startAngle:M_PI_2 endAngle:M_PI clockwise:true];
    [path addLineToPoint:CGPointMake(0, kCornerRadius)];
    [path addArcWithCenter:CGPointMake(kCornerRadius, kCornerRadius) radius:kCornerRadius startAngle:M_PI endAngle:M_PI_2 * 3.0 clockwise:true];
    [path addLineToPoint:CGPointMake(kCircleSideLength, 0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCircleLineWidth)];
    [path closePath];
    
    CAShapeLayer *cPathLayer = [CAShapeLayer layer];
    self.cPathLayer = cPathLayer;
    cPathLayer.path = path.CGPath;
    cPathLayer.fillColor = self.cColor.CGColor;
    cPathLayer.strokeColor = [UIColor redColor].CGColor;
    cPathLayer.lineWidth = 0.0;
    [self.cLayer addSublayer:cPathLayer];
    
    return self;
}

- (void)maskC {
    CALayer *cMaskLayer = [CALayer layer];
    self.cMaskLayer = cMaskLayer;
    cMaskLayer.frame = self.cPathLayer.frame;
    self.cLayer.mask = cMaskLayer;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth / 2.0, 0)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth / 2.0, kCircleSideLength)];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleSideLength - kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCircleLineWidth / 2.0)];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth * 0.5)];
    CAShapeLayer *cMaskPathLayer1 = [CAShapeLayer layer];
    self.cMaskPathLayer1 = cMaskPathLayer1;
    // strokeStart之前的路径全部都不显示出来
    cMaskPathLayer1.strokeStart = 0;
    // strokeEnd之后的路径也不显示出来
    cMaskPathLayer1.strokeEnd = 0;
    cMaskPathLayer1.fillColor = [UIColor greenColor].CGColor;
    cMaskPathLayer1.strokeColor = [UIColor yellowColor].CGColor;
    cMaskPathLayer1.path = path.CGPath;
    cMaskPathLayer1.lineWidth = kCircleLineWidth;
    [cMaskLayer addSublayer:cMaskPathLayer1];
    
    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth / 2.0)];
    CAShapeLayer *cMaskPathLayer2 = [CAShapeLayer layer];
    self.cMaskPathLayer2 = cMaskPathLayer2;
    cMaskPathLayer2.strokeEnd = 0;
    cMaskPathLayer2.fillColor = [UIColor clearColor].CGColor;
    cMaskPathLayer2.strokeColor = [UIColor yellowColor].CGColor;
    cMaskPathLayer2.path = path.CGPath;
    cMaskPathLayer2.lineWidth = kCircleLineWidth;
    [cMaskLayer addSublayer:cMaskPathLayer2];
}

#pragma mark ----- 画F
- (SLYJLodingView *)drawLogoF {
    self.fLayer.frame = CGRectMake(kCircleSideLength - kCircleLineWidth, kCircleSideLength - kCircleLineWidth, kCircleSideLength, kCircleSideLength);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleSideLength, 0)];
    [path addLineToPoint:CGPointMake(kCornerRadius, 0)];
    [path addArcWithCenter:CGPointMake(kCornerRadius, kCornerRadius) radius:kCornerRadius startAngle:-M_PI_2 endAngle:-M_PI clockwise:false];
    [path addLineToPoint:CGPointMake(0, (kCircleSideLength - kCircleLineWidth) * 2)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, (kCircleSideLength - kCircleLineWidth) * 2)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleSideLength)];
    [path addLineToPoint:CGPointMake(kCircleSideLength - kCornerRadius, kCircleSideLength)];
    [path addArcWithCenter:CGPointMake(kCircleSideLength - kCornerRadius, kCircleSideLength - kCornerRadius) radius:kCornerRadius startAngle:M_PI_2 endAngle:0 clockwise:false];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, 0)];
    [path closePath];
    
    CAShapeLayer *fPathLayer = [CAShapeLayer layer];
    self.fPathLayer = fPathLayer;
    fPathLayer.path = path.CGPath;
    fPathLayer.frame = CGRectMake(0, 0, kCircleSideLength, kCircleSideLength);
    fPathLayer.fillColor = self.fColor.CGColor;
    [self.fLayer addSublayer:fPathLayer];
    
    return self;
}

- (void)maskF {
    CALayer *fMaskLayer = [CALayer layer];
    self.fMaskLayer = fMaskLayer;
    fMaskLayer.frame = self.fPathLayer.frame;
    self.fLayer.mask = fMaskLayer;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth / 2.0, 0)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth / 2.0, (kCircleSideLength - kCircleLineWidth) * 2.0)];
    CAShapeLayer *fMaskPathLayer1 = [CAShapeLayer layer];
    self.fMaskPathLayer1 = fMaskPathLayer1;
    fMaskPathLayer1.strokeEnd = 0;
    fMaskPathLayer1.fillColor = [UIColor clearColor].CGColor;
    fMaskPathLayer1.strokeColor = [UIColor yellowColor].CGColor;
    fMaskPathLayer1.path = path.CGPath;
    fMaskPathLayer1.lineWidth = kCircleLineWidth;
    [fMaskLayer addSublayer:fMaskPathLayer1];
    
    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth / 2.0)];
    CAShapeLayer *fMaskPathLayer2 = [CAShapeLayer layer];
    self.fMaskPathLayer2 = fMaskPathLayer2;
    fMaskPathLayer2.strokeEnd = 0;
    fMaskPathLayer2.fillColor = [UIColor clearColor].CGColor;
    fMaskPathLayer2.strokeColor = [UIColor yellowColor].CGColor;
    fMaskPathLayer2.path = path.CGPath;
    fMaskPathLayer2.lineWidth = kCircleLineWidth;
    [fMaskLayer addSublayer:fMaskPathLayer2];
    
    path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleSideLength - kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCircleLineWidth / 2.0)];
    CAShapeLayer *fMaskPathLayer3 = [CAShapeLayer layer];
    self.fMaskPathLayer3 = fMaskPathLayer3;
    fMaskPathLayer3.strokeEnd = 0;
    fMaskPathLayer3.fillColor = [UIColor clearColor].CGColor;
    fMaskPathLayer3.strokeColor = [UIColor yellowColor].CGColor;
    fMaskPathLayer3.path = path.CGPath;
    fMaskPathLayer3.lineWidth = kCircleLineWidth;
    [fMaskLayer addSublayer:fMaskPathLayer3];
}

#pragma mark ----- 画P
- (SLYJLodingView *)drawLogoP {
    self.pLayer.frame = CGRectMake((kCircleSideLength - kCircleLineWidth) * 2.0, 0, kCircleSideLength, kCircleSideLength);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth, (kCircleSideLength - kCircleLineWidth) * 2.0)];
    [path addLineToPoint:CGPointMake(0, (kCircleSideLength - kCircleLineWidth) * 2.0)];
    [path addLineToPoint:CGPointMake(0, kCornerRadius)];
    [path addArcWithCenter:CGPointMake(kCornerRadius, kCornerRadius) radius:kCornerRadius startAngle:-M_PI endAngle:-M_PI_2 clockwise:true];
    [path addLineToPoint:CGPointMake(kCircleSideLength - kCornerRadius, 0)];
    [path addArcWithCenter:CGPointMake(kCircleSideLength - kCornerRadius, kCornerRadius) radius:kCornerRadius startAngle:-M_PI_2 endAngle:0 clockwise:true];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleSideLength - kCornerRadius)];
    [path addArcWithCenter:CGPointMake(kCircleSideLength - kCornerRadius, kCircleSideLength - kCornerRadius) radius:kCornerRadius startAngle:0 endAngle:M_PI_2 clockwise:true];
    [path addLineToPoint:CGPointMake(kCircleLineWidth * 1.3, kCircleSideLength)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth * 1.3, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength - kCircleLineWidth, kCircleSideLength - kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength - kCircleLineWidth, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth)];
    [path closePath];
    
    CAShapeLayer *pPathLayer = [CAShapeLayer layer];
    self.pPathLayer = pPathLayer;
    pPathLayer.path = path.CGPath;
    pPathLayer.frame = CGRectMake(0, 0, kCircleSideLength, kCircleSideLength);
    pPathLayer.fillColor = self.pColor.CGColor;
    [self.pLayer addSublayer:pPathLayer];
    
    return self;
}

- (void)maskP {
    CALayer *pMaskLayer = [CALayer layer];
    self.pMaskLayer = pMaskLayer;
    pMaskLayer.frame = self.pPathLayer.frame;
    self.pLayer.mask = pMaskLayer;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(kCircleLineWidth * 0.5, (kCircleSideLength - kCircleLineWidth) * 2.0)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth * 0.5, 0)];
    [path moveToPoint:CGPointMake(kCircleLineWidth, kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleSideLength, kCircleLineWidth / 2.0)];
    [path moveToPoint:CGPointMake(kCircleSideLength - kCircleLineWidth / 2.0, kCircleLineWidth)];
    [path addLineToPoint:CGPointMake(kCircleSideLength - kCircleLineWidth / 2.0, kCircleSideLength)];
    [path moveToPoint:CGPointMake(kCircleSideLength - kCircleLineWidth, kCircleSideLength - kCircleLineWidth / 2.0)];
    [path addLineToPoint:CGPointMake(kCircleLineWidth * 1.3, kCircleSideLength - kCircleLineWidth / 2.0)];
    
    CAShapeLayer *pMaskPathLayer1 = [CAShapeLayer layer];
    self.pMaskPathLayer1 = pMaskPathLayer1;
    pMaskPathLayer1.strokeEnd = 0;
    pMaskPathLayer1.fillColor = [UIColor clearColor].CGColor;
    pMaskPathLayer1.strokeColor = [UIColor yellowColor].CGColor;
    pMaskPathLayer1.path = path.CGPath;
    pMaskPathLayer1.lineWidth = kCircleLineWidth;
    [pMaskLayer addSublayer:pMaskPathLayer1];
}

#pragma mark ----- 透明度动画
- (void)startAnimation {
    self.isAnimation = true;
    
    self.cMaskPathLayer1.strokeEnd = 1.0;
    self.cMaskPathLayer2.strokeEnd = 1.0;
    self.fMaskPathLayer1.strokeEnd = 1.0;
    self.fMaskPathLayer2.strokeEnd = 1.0;
    self.fMaskPathLayer3.strokeEnd = 1.0;
    self.pMaskPathLayer1.strokeEnd = 1.0;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.fromValue = @1.0;
    alphaAnimation.toValue = @0.3;
    alphaAnimation.repeatCount = MAXFLOAT;
    alphaAnimation.autoreverses = true;
    alphaAnimation.duration = 0.5;
    
    [self.cLayer addAnimation:alphaAnimation forKey:@"alpha"];
    [self.pLayer addAnimation:alphaAnimation forKey:@"alpha"];
    
    alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.fromValue = @0.3;
    alphaAnimation.toValue = @1.0;
    alphaAnimation.repeatCount = MAXFLOAT;
    alphaAnimation.autoreverses = true;
    alphaAnimation.duration = 0.5;
    [self.fLayer addAnimation:alphaAnimation forKey:@"alpha"];
}

- (void)stopAnimation {
    self.cMaskPathLayer1.strokeEnd = 0.0;
    self.cMaskPathLayer2.strokeEnd = 0.0;
    self.fMaskPathLayer1.strokeEnd = 0.0;
    self.fMaskPathLayer2.strokeEnd = 0.0;
    self.fMaskPathLayer3.strokeEnd = 0.0;
    self.pMaskPathLayer1.strokeEnd = 0.0;
    
    [self.cLayer removeAllAnimations];
    [self.fLayer removeAllAnimations];
    [self.pLayer removeAllAnimations];
    
    self.isAnimation = false;
}

#pragma mark ----- 步进动画
- (void)changeMaskWithProgress:(CGFloat)progress {
    CGFloat cMaskPathLayer1Progress = MIN(0.6, MAX(0, progress));
    self.cMaskPathLayer1.strokeEnd = cMaskPathLayer1Progress / 0.6;
    
    CGFloat cMaskPathLayer2Progress = MIN(0.6, MAX(0.2, progress));
    self.cMaskPathLayer2.strokeEnd = (cMaskPathLayer2Progress - 0.2) / 0.4;
    
    CGFloat fMaskPathLayer1Progress = MIN(0.5, MAX(0.2, progress));
    self.fMaskPathLayer1.strokeEnd = (fMaskPathLayer1Progress - 0.2) / 0.3;
    
    CGFloat fMaskPathLayer2Progress = MIN(0.7, MAX(0.4, progress));
    self.fMaskPathLayer2.strokeEnd = (fMaskPathLayer2Progress - 0.4) / 0.3;
    
    CGFloat fMaskPathLayer3Progress = MIN(0.9, MAX(0.6, progress));
    self.fMaskPathLayer3.strokeEnd = (fMaskPathLayer3Progress - 0.6) / 0.3;
    
    CGFloat pMaskPathLayer1Progress = MIN(1, MAX(0.6, progress));
    self.pMaskPathLayer1.strokeEnd = (pMaskPathLayer1Progress - 0.6) / 0.4;
}

#pragma mark ----- Setter和Getter方法
- (void)setProgress:(CGFloat)progress {
    _progress = progress > 1 ? 1 : (progress < 0 ? 0 : progress);
    [self changeMaskWithProgress:_progress];
}

@end
